package com.taotao.manage.controller;



import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.bean.EasyUIResult;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemDesc;
import com.taotao.manage.service.ItemDescService;
import com.taotao.manage.service.ItemService;
@RequestMapping("/item")
@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;
	@Autowired
	private ItemDescService itemDescService;
	
	
	@RequestMapping(method=RequestMethod.POST)
	 public ResponseEntity<Void> saveItem(Item item, @RequestParam("desc") String desc){
		try {
			//TODO进行数据校验
			if (StringUtils.isEmpty(item.getTitle())) {
				//返回参数错误:400
				return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
			}
			//新增商品
			this.itemService.saveItem(item,desc);
			//201返回成功
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500异常
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
	/**
	 * 更新数据
	 * @param item
	 * @param desc
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateItem(Item item, @RequestParam("desc") String desc){
		try {
			//TODO进行数据校验
			if (StringUtils.isEmpty(item.getTitle())) {
				//返回参数错误:400
				return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
			}
			//新增商品
			this.itemService.updateItem(item,desc);
			//201返回成功
			return ResponseEntity .status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500异常
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * 
	 * 分页查询
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryItemList(
			@RequestParam(value="page",defaultValue="1")Integer pageNum,
			@RequestParam(value="rows",defaultValue="10")Integer pageSize
			){
		try {
			Item record = new Item();
			record.setStatus(1);
			//分页查询和排序
			PageInfo<Item> pageInfo = this.itemService.queryPageListByWhere(record , pageNum, pageSize);
			//判断
			if (pageInfo==null || pageInfo.getSize()==0) {
				//没有404
				return  ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			//如果有,分装bean返回 
			EasyUIResult uiResult = new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
			return ResponseEntity.ok(uiResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500异常
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
     * 根据商品ID查询商品描述
     * @param itemId
     * @return
     */
    @RequestMapping(value="desc/{itemId}", method = RequestMethod.GET)
    public ResponseEntity<ItemDesc> queryItemDescByItemId(@PathVariable("itemId") Long itemId){
        try {
            // 查询商品描述信息
            ItemDesc itemDesc = this.itemDescService.queryById(itemId);
            if( itemDesc == null){
                // 如果没有，返回404
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            // 如果有，返回成功200
            return ResponseEntity.ok().body(itemDesc);
        } catch (Exception e) {
            e.printStackTrace();
            // 异常，返回500
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
}
